import tokens from '../token'
const userDB=[{
    id:1,name:'肖清宇',stunum:'201610414422',password:"201610414422",user_type:"2"
},{
  id:2,name:'张jy',stunum:'001',password:"001",user_type:"1"
},{
  id:3,name:'胡dk',stunum:'002',password:"002",user_type:"1"
}]
//user_type:角色类型
export default[
    {
    path: '/api/login',
    method: 'post',
    handle ({ body }) {
      const user = userDB.find(e => e.stunum === body.stunum && e.password === body.password)
      if (user) {
        const token=`dfghjk${Number(new Date())}`
        tokens[token] = user
        return {
          code: 0,
          msg: '登录成功',
          data: {
            user,
            token
          }
        }
      } else {
        return {
          code: 401,
          msg: '用户名或密码错误',
          data: {}
        }
      }
    }
  },{
    path: '/api/login/out',
    method: 'post',
    handle ({ body }) {
      const user=tokens[body.token]
      if (user) {
        return {
          code: 0,
          msg: '注销成功',
          data: {}
        }
      } else {
        return {
          code: 500,
          msg: '登录失效，请重新登录',
          data: {}
        }
      }
    }
  } 
]